﻿using System.Text;
using System.Linq;
using System;
using System.Collections.Generic;
using Elderos.Highlight.Search;

namespace Elderos.Highlight
{
    public class SimpleAmbiguityResolver : ThresholdAmbiguityResolver
    {
    	public SimpleAmbiguityResolver(double activationThreshold)
			: base(activationThreshold)
    	{}

        public override void Process(IEnumerable<Position> positions)
        {
            foreach (var entityInfo in positions)
                ProcessPosition(entityInfo);
        }

        public override IAmbiguityResolver Clone()
        {
            return new SimpleAmbiguityResolver(ActivationThreshold);
        }

        private void ProcessPosition(Position entityInfo)
        {
            double? maxCoef = null;
            foreach (TextEntity textEntity in entityInfo.Entities)
            {
                if (!maxCoef.HasValue || textEntity.ResultCoef > maxCoef)
                {
                    maxCoef = textEntity.ResultCoef;
                    if (maxCoef > ActivationThreshold)
                        entityInfo.Winner = textEntity;
                }
                else if (textEntity.ResultCoef == maxCoef)
                    entityInfo.Winner = null;
            }
        }
    }
}
